<?php
FLEA::loadClass('TMIS_TableDataGateway');
class Model_Jichu_ProKind extends TMIS_TableDataGateway {
    var $tableName = "jichu_prokind";
    var $primaryKey = "id";
    var $primaryName = "kindName";
    var $needCreateLog = true;// 需要打日志则加此参数
    var $moduleName = '产品大类';// 模块名称
    var $arrayType = array(
            'text'         =>'手工输入',
            'autocomplete' =>'可选可输',
        );

    var $hasMany = array (
        array(
            'tableClass'  => 'Model_Jichu_PrintConfig',
            'foreignKey'  => 'kindId',
            'mappingName' => 'PrintCfg',
            'sort'        => 'id'
        )
    );
    //产品大类
    function getOptions(){
        $str="select * from jichu_prokind";
        $row = $this->findBySql($str);
        foreach($row as & $v){
            $arr[]=array('value'=>$v[$this->primaryKey],'text'=>$v[$this->primaryName]);
        }
        return $arr;
    }

    //表格中需要展示的单独列
    function fileds2Table($kindId){
        $rows = $this->getFileds($kindId);
        $cols = array();
        foreach ($rows['single'] as & $v) {
            //键值唯一，使用Kindid和字段名重新命名
            // $cols[$v['filed'].'_'.$v['kindId']] = $v['title'];
            $key = 'filed_'.$v['title'];
            $cols[$key] = $v['title'];
        }
        return $cols;
    }

    //表格中需要展示的单独列
    function fileds2TableNew($kindId){
        $rows = $this->getFileds($kindId);
        $cols = array();
        foreach ($rows['single'] as & $v) {
            $v['colWidth'] = $v['colWidth'] ? $v['colWidth'] : 1;
            //键值唯一，使用Kindid和字段名重新命名
            // $cols[$v['filed'].'_'.$v['kindId']] = $v['title'];
            $key = 'filed_'.$v['title'];
            $cols[$key] = array('text'=>$v['title'],'width'=>$v['colWidth']*100);
        }
        return $cols;
    }

    //表格中需要展示的单独列
    function filedsSingleGroupKind($type = 'single'){
        $rows = $this->getFileds();
        $cols = array();
        foreach ($rows[$type] as & $v) {
            $v['filedKey'] = 'filed_'.$v['title'];
            $cols[$v['kindId']][] = $v;
        }
        return $cols;
    }

    //需要展示的字段
    function getFileds($kindId ,$type=''){
        $condition[] = array('extendAttr',"''",'<>');
        if($kindId > 0){
            $condition['id'] = $kindId;
        }
        $allKinds = $this->findAll($condition);
        // dump($allKinds);exit;
        $allFileds = array();
        foreach ($allKinds as $key => &$v) {
            if($v['extendAttr']){
                $tmp = json_decode($v['extendAttr'] ,1);
                foreach ($tmp as & $f) {
                    $f['kindId'] = $v['id'];
                    if($f['title'] && $f['status'] =='on'){
                        //展示所有的，不分类
                        if($type == 'all'){
                            $allFileds[] = $f;
                        }else{
                            if($f['showType'] == 'single'){
                                $allFileds['single'][] = $f;
                            }else{
                                $allFileds['default'][] = $f;
                            }
                        }
                    }
                }
            }
        }

        $allFileds['single'] && $allFileds['single'] =array_sortby_multifields($allFileds['single'] ,array('showSort'=>SORT_DESC,'filed'=>SORT_ASC));
        $allFileds['default'] && $allFileds['default']=array_sortby_multifields($allFileds['default'] ,array('showSort'=>SORT_DESC,'filed'=>SORT_ASC));
        if($type == 'all'){
            $allFileds = array_sortby_multifields($allFileds ,array('showSort'=>SORT_DESC,'filed'=>SORT_ASC));
        }
        // dump($allFileds);exit;
        return $allFileds;
    }

    /**
     * 获取所有的字段，字段展示为filed1=>门幅
     * Time：2019/11/08 10:27:02
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getFiledsKeyValue($kindId ,$type=''){
        $kindList = $this->getFileds($kindId ,$type);
        $list = array();
        foreach ($kindList as $key => & $field) {
            foreach($field as & $v){
                $list[$v['filed']] = $v['title'];
            }
        }

        return $list;
    }

    /**
     * 获取所有的字段，字段展示为filed1=>门幅
     * Time：2019/11/08 10:27:02
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getFiledsKeyValueWidth($kindId ,$type=''){
        $kindList = $this->getFileds($kindId ,$type);
        $list = array();
        foreach ($kindList as $key => & $v) {
            $v['colWidth'] = $v['colWidth'] ? $v['colWidth'] : 1;
            $list[$v['filed']] = array('text'=>$v['title'],'width'=>$v['colWidth']*100);
        }

        return $list;
    }


}
?>